package com.visz.tv.utils;

import android.content.Context;
import android.content.res.Resources;

import com.visz.tv.BuildConfig;
import com.visz.tv.Channel;
import com.visz.tv.R;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;

public class ChannelUtil {
   private static String IPV6_CHANNEL =
    "CCTV-1 综合,http://172.31.203.136:8006/jscn/hd1cctv1.m3u8\n"+
    "CCTV-2 财经,http://172.31.203.136:8006/jscn/hd1cctv2.m3u8?UserId=89351121400000005&app=01&livemode=1&assetID=hd1cctv2&isAd=0&userCode=89351121400000005&devicetype=6&fvTraceId=1c29f3b0002399ae\n"+
    "CCTV-3 综艺,http://172.31.203.136:8006/jscn/hd1cctv3.m3u8?UserId=89351121400000005&app=01&livemode=1&assetID=hd1cctv3&isAd=0&userCode=89351121400000005&devicetype=6&fvTraceId=1c29f4100078fb77\n"+
    "CCTV-4 中文国际,http://172.31.203.136:8006/jscn/hd1cctv4.m3u8?UserId=89351121400000005&app=01&livemode=1&assetID=hd1cctv4&isAd=0&userCode=89351121400000005&devicetype=6&fvTraceId=1cab2d2a004d90b8\n"+
    "CCTV-4K 中文国际高清,http://172.31.203.136:8006/jscn/CCTV-4K.m3u8?UserId=89351121400000005&app=01&livemode=1&assetID=CCTV-4K&isAd=0&userCode=89351121400000005&devicetype=6&fvTraceId=1c9de5d800647df7\n"+
    "CCTV-5 体育,http://172.31.203.136:8006/jscn/hd1cctv5.m3u8?UserId=89351121400000005&app=01&livemode=1&assetID=hd1cctv5&isAd=0&userCode=89351121400000005&devicetype=6&fvTraceId=1cab2dc6005a0f6b\n"+
    "CCTV-5+ 体育赛事,http://172.31.203.136:8006/jscn/CCTV-5PLUSHD.m3u8?UserId=89351121400000005&app=01&livemode=1&assetID=CCTV-5PLUSHD&isAd=0&userCode=89351121400000005&devicetype=6&fvTraceId=1cab3132001fc497\n"+
    "CCTV-6 电影,http://172.31.203.136:8006/jscn/hd1cctv6.m3u8?UserId=89351121400000005&app=01&livemode=1&assetID=hd1cctv6&isAd=0&userCode=89351121400000005&devicetype=6&fvTraceId=1cab35f40037219d\n"+
    "CCTV-7 国防军事,http://172.31.203.136:8006/jscn/hd1cctv7.m3u8?UserId=89351121400000005&app=01&livemode=1&assetID=hd1cctv7&isAd=0&userCode=89351121400000005&devicetype=6&fvTraceId=1cab387e0037aec3\n"+
    "CCTV-8 电视剧,http://172.31.203.136:8006/jscn/hd1cctv8.m3u8?UserId=89351121400000005&app=01&livemode=1&assetID=hd1cctv8&isAd=0&userCode=89351121400000005&devicetype=6&fvTraceId=1cab39860007373a\n"+
    "CCTV-9 纪录,http://172.31.203.136:8006/jscn/CCTV-9HD.m3u8?UserId=89351121400000005&app=01&livemode=1&assetID=CCTV-9HD&isAd=0&userCode=89351121400000005&devicetype=6&fvTraceId=1cab3e9c00667635\n"+
    "CCTV-10 科教,http://172.31.203.136:8006/jscn/CCTV-10HD.m3u8?UserId=89351121400000005&app=01&livemode=1&assetID=CCTV-10HD&isAd=0&userCode=89351121400000005&devicetype=6&fvTraceId=1cab40180057df08\n"+
    "CCTV-11 戏曲,http://172.31.203.136:8006/jscn/hd1cctv11.m3u8?UserId=89351121400000005&app=01&livemode=1&assetID=hd1cctv11&isAd=0&userCode=89351121400000005&devicetype=6&fvTraceId=1cab45ee007cc921\n"+
    "CCTV-12 社会与法,http://172.31.203.136:8006/jscn/CCTV-12HD.m3u8?UserId=89351121400000005&app=01&livemode=1&assetID=CCTV-12HD&isAd=0&userCode=89351121400000005&devicetype=6&fvTraceId=1cab466a0029671c\n"+
    "CCTV-13 新闻,http://172.31.203.136:8006/jscn/hd1cctv13.m3u8?UserId=89351121400000005&app=01&livemode=1&assetID=hd1cctv13&isAd=0&userCode=89351121400000005&devicetype=6&fvTraceId=1cab46d00073aa51\n"+
    "CGTN,http://172.31.203.136:8006/jscn/CGTNHD.m3u8?UserId=89351121400000005&app=01&livemode=1&assetID=CGTNHD&isAd=0&userCode=89351121400000005&devicetype=6&fvTraceId=1cab49940068c030\n"+
    "江苏卫视,http://172.31.203.136:8006/jscn/hd1jsws.m3u8?UserId=89351121400000005&app=01&livemode=1&assetID=hd1jsws&isAd=0&userCode=89351121400000005&devicetype=6&fvTraceId=1cad0dd000421645\n"+
    "北京卫视,http://172.31.203.136:8006/jscn/BeiJingTVHD.m3u8?UserId=89351121400000005&app=01&livemode=1&assetID=BeiJingTVHD&isAd=0&userCode=89351121400000005&devicetype=6&fvTraceId=1cad0ed6005ee958\n"+
    "湖南卫视,http://219.151.31.38/liveplay-kk.rtxapp.com/live/program/live/hnwshd/4000000/mnf.m3u8\n"+
    "浙江卫视,http://172.31.203.136:8006/jscn/ZheJiangTVHD.m3u8?UserId=89351121400000005&app=01&livemode=1&assetID=ZheJiangTVHD&isAd=0&userCode=89351121400000005&devicetype=6&fvTraceId=1cad113a005d5166\n"+
    "东方卫视,http://172.31.203.136:8006/jscn/hd1dfws.m3u8?UserId=89351121400000005&app=01&livemode=1&assetID=hd1dfws&isAd=0&userCode=89351121400000005&devicetype=6&fvTraceId=1cad12c800340e94\n"+
    "黑龙江卫视,http://172.31.203.136:8006/jscn/HeiLongJiangTVHD.m3u8?UserId=89351121400000005&app=01&livemode=1&assetID=HeiLongJiangTVHD&isAd=0&userCode=89351121400000005&devicetype=6&fvTraceId=1cbb4ca2002366e0\n"+
    "深圳卫视,http://172.31.203.136:8006/jscn/ShenZhenTVHD.m3u8?UserId=89351121400000005&app=01&livemode=1&assetID=ShenZhenTVHD&isAd=0&userCode=89351121400000005&devicetype=6&fvTraceId=1cbb4d3e006ab7c8\n"+
    "广东卫视,http://172.31.203.136:8006/jscn/GuangDongTVHD.m3u8?UserId=89351121400000005&app=01&livemode=1&assetID=GuangDongTVHD&isAd=0&userCode=89351121400000005&devicetype=6&fvTraceId=1cbb4d860018b614\n"+
    "辽宁卫视,http://172.31.203.136:8006/jscn/LiaoNingTVHD.m3u8?UserId=89351121400000005&app=01&livemode=1&assetID=LiaoNingTVHD&isAd=0&userCode=89351121400000005&devicetype=6&fvTraceId=1cbb4e420027d25a\n"+
    "重庆卫视,http://172.31.203.136:8006/jscn/ChongQingTVHD.m3u8?UserId=89351121400000005&app=01&livemode=1&assetID=ChongQingTVHD&isAd=0&userCode=89351121400000005&devicetype=6&fvTraceId=1cbb4e9a001b4586\n"+
    "山东卫视,http://172.31.203.136:8006/jscn/ShanDongTVHD.m3u8?UserId=89351121400000005&app=01&livemode=1&assetID=ShanDongTVHD&isAd=0&userCode=89351121400000005&devicetype=6&fvTraceId=1cbb4edc004a0777\n"+
    "天津卫视,http://172.31.203.136:8006/jscn/TianJinTVHD.m3u8?UserId=89351121400000005&app=01&livemode=1&assetID=TianJinTVHD&isAd=0&userCode=89351121400000005&devicetype=6&fvTraceId=1cbb4f1a00630155\n"+
    "安徽卫视,http://172.31.203.136:8006/jscn/AnhuiTVHD.m3u8?\n"+
     "CCTV-8K 超高清,http://ott.mobaibox.com/PLTV/3/224/3221228165/index.m3u8\n"+
    "测试,http://devimages.apple.com/iphone/samples/bipbop/gear3/prog_index.m3u8";
    ;
    private static String IPV4_CHANNEL =  "CCTV-1 综合,http://172.31.203.136:8006/jscn/hd1cctv1.m3u8\n"+
    "CCTV-2 财经,http://172.31.203.136:8006/jscn/hd1cctv2.m3u8?UserId=89351121400000005&app=01&livemode=1&assetID=hd1cctv2&isAd=0&userCode=89351121400000005&devicetype=6&fvTraceId=1c29f3b0002399ae\n"+
    "CCTV-3 综艺,http://172.31.203.136:8006/jscn/hd1cctv3.m3u8?UserId=89351121400000005&app=01&livemode=1&assetID=hd1cctv3&isAd=0&userCode=89351121400000005&devicetype=6&fvTraceId=1c29f4100078fb77\n"+
    "CCTV-4 中文国际,http://172.31.203.136:8006/jscn/hd1cctv4.m3u8?UserId=89351121400000005&app=01&livemode=1&assetID=hd1cctv4&isAd=0&userCode=89351121400000005&devicetype=6&fvTraceId=1cab2d2a004d90b8\n"+
    "CCTV-4K 中文国际高清,http://172.31.203.136:8006/jscn/CCTV-4K.m3u8?UserId=89351121400000005&app=01&livemode=1&assetID=CCTV-4K&isAd=0&userCode=89351121400000005&devicetype=6&fvTraceId=1c9de5d800647df7\n"+
    "CCTV-5 体育,http://172.31.203.136:8006/jscn/hd1cctv5.m3u8?UserId=89351121400000005&app=01&livemode=1&assetID=hd1cctv5&isAd=0&userCode=89351121400000005&devicetype=6&fvTraceId=1cab2dc6005a0f6b\n"+
    "CCTV-5+ 体育赛事,http://172.31.203.136:8006/jscn/CCTV-5PLUSHD.m3u8?UserId=89351121400000005&app=01&livemode=1&assetID=CCTV-5PLUSHD&isAd=0&userCode=89351121400000005&devicetype=6&fvTraceId=1cab3132001fc497\n"+
    "CCTV-6 电影,http://172.31.203.136:8006/jscn/hd1cctv6.m3u8?UserId=89351121400000005&app=01&livemode=1&assetID=hd1cctv6&isAd=0&userCode=89351121400000005&devicetype=6&fvTraceId=1cab35f40037219d\n"+
    "CCTV-7 国防军事,http://172.31.203.136:8006/jscn/hd1cctv7.m3u8?UserId=89351121400000005&app=01&livemode=1&assetID=hd1cctv7&isAd=0&userCode=89351121400000005&devicetype=6&fvTraceId=1cab387e0037aec3\n"+
    "CCTV-8 电视剧,http://172.31.203.136:8006/jscn/hd1cctv8.m3u8?UserId=89351121400000005&app=01&livemode=1&assetID=hd1cctv8&isAd=0&userCode=89351121400000005&devicetype=6&fvTraceId=1cab39860007373a\n"+
    "CCTV-9 纪录,http://172.31.203.136:8006/jscn/CCTV-9HD.m3u8?UserId=89351121400000005&app=01&livemode=1&assetID=CCTV-9HD&isAd=0&userCode=89351121400000005&devicetype=6&fvTraceId=1cab3e9c00667635\n"+
    "CCTV-10 科教,http://172.31.203.136:8006/jscn/CCTV-10HD.m3u8?UserId=89351121400000005&app=01&livemode=1&assetID=CCTV-10HD&isAd=0&userCode=89351121400000005&devicetype=6&fvTraceId=1cab40180057df08\n"+
    "CCTV-11 戏曲,http://172.31.203.136:8006/jscn/hd1cctv11.m3u8?UserId=89351121400000005&app=01&livemode=1&assetID=hd1cctv11&isAd=0&userCode=89351121400000005&devicetype=6&fvTraceId=1cab45ee007cc921\n"+
    "CCTV-12 社会与法,http://172.31.203.136:8006/jscn/CCTV-12HD.m3u8?UserId=89351121400000005&app=01&livemode=1&assetID=CCTV-12HD&isAd=0&userCode=89351121400000005&devicetype=6&fvTraceId=1cab466a0029671c\n"+
    "CCTV-13 新闻,http://172.31.203.136:8006/jscn/hd1cctv13.m3u8?UserId=89351121400000005&app=01&livemode=1&assetID=hd1cctv13&isAd=0&userCode=89351121400000005&devicetype=6&fvTraceId=1cab46d00073aa51\n"+
    "CGTN,http://172.31.203.136:8006/jscn/CGTNHD.m3u8?UserId=89351121400000005&app=01&livemode=1&assetID=CGTNHD&isAd=0&userCode=89351121400000005&devicetype=6&fvTraceId=1cab49940068c030\n"+
    "江苏卫视,http://172.31.203.136:8006/jscn/hd1jsws.m3u8?UserId=89351121400000005&app=01&livemode=1&assetID=hd1jsws&isAd=0&userCode=89351121400000005&devicetype=6&fvTraceId=1cad0dd000421645\n"+
    "北京卫视,http://172.31.203.136:8006/jscn/BeiJingTVHD.m3u8?UserId=89351121400000005&app=01&livemode=1&assetID=BeiJingTVHD&isAd=0&userCode=89351121400000005&devicetype=6&fvTraceId=1cad0ed6005ee958\n"+
    "湖南卫视,http://219.151.31.38/liveplay-kk.rtxapp.com/live/program/live/hnwshd/4000000/mnf.m3u8\n"+
    "浙江卫视,http://172.31.203.136:8006/jscn/ZheJiangTVHD.m3u8?UserId=89351121400000005&app=01&livemode=1&assetID=ZheJiangTVHD&isAd=0&userCode=89351121400000005&devicetype=6&fvTraceId=1cad113a005d5166\n"+
    "东方卫视,http://172.31.203.136:8006/jscn/hd1dfws.m3u8?UserId=89351121400000005&app=01&livemode=1&assetID=hd1dfws&isAd=0&userCode=89351121400000005&devicetype=6&fvTraceId=1cad12c800340e94\n"+
    "黑龙江卫视,http://172.31.203.136:8006/jscn/HeiLongJiangTVHD.m3u8?UserId=89351121400000005&app=01&livemode=1&assetID=HeiLongJiangTVHD&isAd=0&userCode=89351121400000005&devicetype=6&fvTraceId=1cbb4ca2002366e0\n"+
    "深圳卫视,http://172.31.203.136:8006/jscn/ShenZhenTVHD.m3u8?UserId=89351121400000005&app=01&livemode=1&assetID=ShenZhenTVHD&isAd=0&userCode=89351121400000005&devicetype=6&fvTraceId=1cbb4d3e006ab7c8\n"+
    "广东卫视,http://172.31.203.136:8006/jscn/GuangDongTVHD.m3u8?UserId=89351121400000005&app=01&livemode=1&assetID=GuangDongTVHD&isAd=0&userCode=89351121400000005&devicetype=6&fvTraceId=1cbb4d860018b614\n"+
    "辽宁卫视,http://172.31.203.136:8006/jscn/LiaoNingTVHD.m3u8?UserId=89351121400000005&app=01&livemode=1&assetID=LiaoNingTVHD&isAd=0&userCode=89351121400000005&devicetype=6&fvTraceId=1cbb4e420027d25a\n"+
    "重庆卫视,http://172.31.203.136:8006/jscn/ChongQingTVHD.m3u8?UserId=89351121400000005&app=01&livemode=1&assetID=ChongQingTVHD&isAd=0&userCode=89351121400000005&devicetype=6&fvTraceId=1cbb4e9a001b4586\n"+
    "山东卫视,http://172.31.203.136:8006/jscn/ShanDongTVHD.m3u8?UserId=89351121400000005&app=01&livemode=1&assetID=ShanDongTVHD&isAd=0&userCode=89351121400000005&devicetype=6&fvTraceId=1cbb4edc004a0777\n"+
    "天津卫视,http://172.31.203.136:8006/jscn/TianJinTVHD.m3u8?UserId=89351121400000005&app=01&livemode=1&assetID=TianJinTVHD&isAd=0&userCode=89351121400000005&devicetype=6&fvTraceId=1cbb4f1a00630155\n"+
    "安徽卫视,http://172.31.203.136:8006/jscn/AnhuiTVHD.m3u8?\n"+
    "CCTV-8K 超高清,http://ott.mobaibox.com/PLTV/3/224/3221228165/index.m3u8\n"+
    "测试,http://devimages.apple.com/iphone/samples/bipbop/gear3/prog_index.m3u8";;

    public static List<Channel> getChannelList(Context context) {
        if (BuildConfig.FLAVOR.equals("IPV6")) {
            return parseString2ChannelList(IPV6_CHANNEL);
        } else if (BuildConfig.FLAVOR.equals("IPV4")) {
            return parseString2ChannelList(IPV4_CHANNEL);
        } else
            if (BuildConfig.FLAVOR.equals("CNTV")) {
           List<Channel> list1 = getCNTVChannelList(context, R.raw.cntv_channels);
             //  LinkedHashSet<Channel>list1=getCNTVChannelList(context,R.raw.cntv_channels);//去重
             //   HashSet<Channel> hs = new HashSet<Channel>(list1);
            List<Channel> list2 = parseString2ChannelList(IPV6_CHANNEL);
            List<Channel> list3 = parseString2ChannelList(IPV4_CHANNEL);
           list2.addAll(list3);
            for (Channel c2 : list2) {
                boolean has = false;
                for (Channel c1 : list1) {
                    if (c1.name.equals(c2.name)) {
                        if (!c1.backupUrl.contains(c2.url)) {
                            c1.backupUrl.add(0, c2.url);
                        }
                        has = true;
                    }
                }
                if (!has) {
                    list1.add(20, c2);
                }
            }
            int num = 1;
            for (Channel c1 : list1) {
                c1.num = String.format(Locale.getDefault(), "%03d", num);
                num++;
            }
            return list1;
        } else {
            return new ArrayList<>();
        }
    }

    public static List<Channel> parseString2ChannelList(String channelData) {
        String[] ss = channelData.split("\n");
        List<Channel> channels = new ArrayList<>();
        int channelNum = 1;
        for (String s : ss) {
            String[] cc = s.split(",");
            if (cc.length >= 2) {
                if (cc[1].contains("m3u8")) {
                    Channel channel = new Channel();
                    channel.num = String.format("%03d", channelNum);
                    channelNum++;
                    channel.name = cc[0];
                    channel.name = channel.name.replace(" ", "\n");//为了换行好看
                    channel.url = cc[1];
                    channels.add(channel);
                }
            }
        }
        return channels;
    }

    public static String readRaw(Context context, int resId) {
        StringBuilder content = new StringBuilder();
        Resources resources = context.getResources();
        try (InputStream is = resources.openRawResource(resId);
             BufferedReader reader = new BufferedReader(new InputStreamReader(is))) {
            String line;
            while ((line = reader.readLine()) != null) {
                content.append(line).append('\n');
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return content.toString();
    }

    public static List<Channel> parseM3U2ChannelList(String m3uContent) {
        List<Channel> channels = new ArrayList<>();
        HashSet<Channel> hs = new HashSet<Channel>(channels);//去重
        try (BufferedReader reader = new BufferedReader(new StringReader(m3uContent))) {
            String line;
            Channel channel = new Channel();
            int channelNum = 1;
            while ((line = reader.readLine()) != null) {
                if (line.startsWith("#EXTINF")) {
                    channel = new Channel();
                    String[] parts = line.split(",", 2);
                    String infoPart = parts[0];
                    String[] attributes = infoPart.split(" ");
                    for (String attribute : attributes) {
                        if (attribute.startsWith("tvg-id")) {
                            channel.tvgId = attribute.split("=")[1].replaceAll("\"", "");
                        } else if (attribute.startsWith("tvg-name")) {
                            channel.tvgName = attribute.split("=")[1].replaceAll("\"", "");
                        } else if (attribute.startsWith("tvg-logo")) {
                            channel.tvgLogo = attribute.split("=")[1].replaceAll("\"", "");
                        } else if (attribute.startsWith("group-title")) {
                            channel.groupTitle = attribute.split("=")[1].replaceAll("\"", "");
                        }
                    }
                    channel.name = parts[1];
                    channel.name = channel.name.replace(" ", "\n");//为了换行好看
                } else if (line.startsWith("http")) {
                    channel.url = line;
                    channel.num = String.format("%03d", channelNum);
                    channelNum++;
                    channels.add(channel);
                    LogUtil.i(channel.name.replace("\n", " ") + "," + channel.url);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return channels;
    }

    public static List<Channel> getCNTVChannelList(Context context, int resId) {
        List<Channel> channels = new ArrayList<>();
        HashSet<Channel> hs = new HashSet<Channel>(channels);//去重
        try {
            JSONObject json = new JSONObject(readRaw(context, resId));
            JSONObject source = json.getJSONObject("sources");
            int num = 1;
            for (Iterator<String> it = source.keys(); it.hasNext(); ) {
                String ch = it.next();
                JSONArray array = source.getJSONArray(ch);
                if (array.length() > 0) {
                    Channel channel = new Channel();
                    channel.url = array.getString(0).replace("https", "http");
                    channel.name = ch.replace(" ", "\n");
                    channel.num = String.format(Locale.getDefault(), "%03d", num);
                    for (int i = 0; i < array.length(); i++) {
                        channel.backupUrl.add(array.getString(i));
                    }
                    channels.add(channel);
                    LogUtil.i(channel.name.replace("\n", " ") + "," + channel.backupUrl.toString());
                    num++;
                }

            }
        } catch (JSONException e) {
            throw new RuntimeException(e);
        }
        return channels;

    }
}
